﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// GetTPB, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
namespace GetTpb
{
    public class TpbItem
    {
        //
        private string _Title = "";
        private string _Page = "";
     
        private string _Torrent = "";
        private string _Magnet = "";
        private DateTime _Uploaded = DateTime.Now;
        private double _Size = 0.0;
        private int _Seeders = 0;
        private int _Leechers = 0;
        //
        public TpbItem()
        {
            Set("", "", "", "", "", "", "", "");
        }
        //
        public TpbItem(string __Title, string __Page, string __Torrent, string __Magnet, string __Uploaded, string __Size, string __Seeders, string __Leechers)
        {
            Set(__Title, __Page, __Torrent, __Magnet, __Uploaded, __Size, __Seeders, __Leechers);
        }
        //
        public void Set(string __Title, string __Page, string __Torrent, string __Magnet, string __Uploaded, string __Size, string __Seeders, string __Leechers)
        {
            Title = __Title;
            Page = __Page;
            Torrent = __Torrent;
            Magnet = __Magnet;
            Uploaded = strUploaded(__Uploaded);
            Size = strSize(__Size);
            Seeders = strSeeders(__Seeders);
            Leechers = strLeechers(__Leechers);
            //
            return;
        }
        //
        public string Title
        {
            get
            {
                return _Title;
            }
            set
            {
                _Title = value.Trim();
            }
        }
        //
        public string Page
        {
            get
            {
                return _Page;
            }
            set
            {
                _Page = value.Trim();
            }
        }
        //

        //
        public string Torrent
        {
            get
            {
                return _Torrent;
            }
            set
            {
                _Torrent = value.Trim();
            }
        }
        //
        public string Magnet
        {
            get
            {
                return _Magnet;
            }
            set
            {
                _Magnet = value.Trim();
            }
        }        //
        //
        public DateTime Uploaded
        {
            get
            {
                return _Uploaded;
            }
            set
            {
                _Uploaded = value;
            }
        }
        public DateTime strUploaded(string s)
        {
            _Uploaded = TpbUtils.FromPageDate2DT(s);
            return _Uploaded;
        }

        public double Size
        {
            get
            {
                return _Size;
            }
            set
            {
                _Size = value;
            }
        }
        //
        public double strSize(string s)
        {
            _Size = TpbUtils.FromPageSize2Dbl(s);
            return _Size;
        }
        //
        //
        public int Seeders
        {
            get
            {
                return _Seeders;
            }
            set
            {
                _Seeders = value;
            }
        }
        //
        public int strSeeders(string s)
        {
            int n;
            if (int.TryParse(s, out n))
            {
                _Seeders = n;
            }
            return _Seeders;
        }
        //
        public int Leechers
        {
            get
            {
                return _Leechers;
            }
            set
            {
                _Leechers = value;
            }
        }
        //
        public int strLeechers(string s)
        {
            int n;
            if (int.TryParse(s, out n))
            {
                _Leechers = n;
            }
            return _Leechers;
        }
        //
        //
        //
        //
        //
        //
        //
        //
        public string GetHtmlTableRow()
        {
            string s = "";
            //
            //<tr><td><font face="VERDANA"><a href="internet8/default.htm">Komputer internetowy na karcie CF</a></font></td></tr>
            s = "<tr>";
            //
            s += string.Format("<td><a href=\"{1}\">{0}</a></td>", Title, Page);
            s += string.Format("<td align=\"center\">{0}</td>", Uploaded.ToString("yy-MM-dd HH:mm"));
            if ("" == Torrent)
            {
                s += string.Format("<td align=\"center\"><a href=\"{1}\">Magnet</a></td>", Torrent, Magnet);
            }
            else
            {
                s += string.Format("<td align=\"center\"><a href=\"{0}\">Torrent</a>&nbsp;<a href=\"{1}\">Magnet</a></td>", Torrent, Magnet);
            }
            s += string.Format("<td align=\"right\">{0}</td>", Size.ToString("N1"));
            s += string.Format("<td align=\"right\">{0}</td>", Seeders);
            s += string.Format("<td align=\"right\">{0}</td>", Leechers);
            //
            s += "</tr>";
            //
            return s;
        }
        //
        //
        public static string GetHtmlBegin(string title)
        {
            string s = "";
            s += "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">";
            s += "<html>";
            s += "<head>";
            s += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>";
            s += "<title>" + title + "</title>";
            s += "<body>";
            s += "<center>";
            s += "<font face=\"tahoma\">";
            s += "<table border=\"2\" cellspacing=\"0\" width=\"90%\">";
            s += "<tbody>";
            //
            s += "<tr>";
            s += "<th>Page</th>";
            s += "<th>Uploaded</th>";
            s += "<th>Torrent&nbsp;Magnet</th>";
            s += "<th>MB</th>";
            s += "<th>SE</th>";
            s += "<th>LE</th>";
            s += "</tr>";
            //
            return s;
        }
        //
        public static string GetHtmlEnd()
        {
            string s = "";
            s += "</tbody>";
            s += "</table>";
            s += "</font>";
            s += "</center>";
            s += "</body>";
            s += "</html>";
            return s;
        }
        //
        //
        //
        public override string ToString()
        {
            return string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|", Title,Page,Torrent,Magnet,Uploaded,Size,Seeders,Leechers);
        }
    }//class
}//namespace
